package com.d3works.apps.businessproject.service;

import com.d3works.apps.account.db.Invoice;
import com.d3works.apps.base.service.DurationService;
import com.d3works.apps.base.service.app.AppBaseService;
import com.d3works.apps.base.service.tax.TaxService;
import com.d3works.apps.contract.db.Contract;
import com.d3works.apps.contract.db.repo.ConsumptionLineRepository;
import com.d3works.apps.contract.db.repo.ContractLineRepository;
import com.d3works.apps.contract.db.repo.ContractRepository;
import com.d3works.apps.contract.service.ContractLineService;
import com.d3works.apps.contract.service.ContractServiceImpl;
import com.d3works.apps.contract.service.ContractVersionService;
import com.d3works.apps.project.db.Project;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;

public class ProjectContractServiceImpl extends ContractServiceImpl {

  @Inject
  public ProjectContractServiceImpl(
      AppBaseService appBaseService,
      ContractVersionService versionService,
      ContractLineService contractLineService,
      DurationService durationService,
      ContractLineRepository contractLineRepo,
      ConsumptionLineRepository consumptionLineRepo,
      ContractRepository contractRepository,
      TaxService taxService) {
    super(
        appBaseService,
        versionService,
        contractLineService,
        durationService,
        contractLineRepo,
        consumptionLineRepo,
        contractRepository,
        taxService);
  }

  @Override
  public Invoice generateInvoice(Contract contract) throws D3worksException {
    Invoice invoice = super.generateInvoice(contract);
    Project project = contract.getProject();

    if (project != null && project.getIsBusinessProject()) {
      invoice.setProject(project);
    }

    return invoice;
  }
}
